package org.batatasframework.bytecode;

import java.lang.reflect.Method;

import org.batatasframework.bytecode.spi.AsmLoadable;
import org.springframework.asm.MethodVisitor;

import cn.bonoon.kernel.descriptors.ClassPropertyAccessor;

/**
 * 一个属性，包括对应的变量和对应的get/set方法
 * @author jackson
 *
 */
public class PropertyAsminfo {
	
	/** 属性对应的类 */
	private final ClassAsminfo owner;
//	/** 属性所属的本地变量 */
//	private final LocalVariableAsminfo localVariable;
	/** 类定义的属性的访问器 */
	private final ClassPropertyAccessor propertyAccessor;
	private MethodAsminfo getter;
	private MethodAsminfo setter;
	
	public PropertyAsminfo(ClassAsminfo owner/*, LocalVariableAsminfo localVariable*/, ClassPropertyAccessor propertyAccessor){
		this.owner = owner;
//		this.localVariable = localVariable;
		this.propertyAccessor = propertyAccessor;
		
		Method setter = propertyAccessor.setter();
		Method getter = propertyAccessor.getter();
		if(null != setter){
			this.setter = new MethodAsminfo(owner, setter);
		}
		if(null != getter){
			this.getter = new MethodAsminfo(owner, getter);
		}
	}
	
	public ClassAsminfo getOwner() {
		return owner;
	}
	
	public ClassPropertyAccessor getPropertyAccessor() {
		return propertyAccessor;
	}
	
	public AsmMethodLoader loadGet(LocalVariableAsminfo localVariable){
		return new AsmMethodLoader(localVariable, getter);
	}
	
	public void invokeSet(MethodVisitor visitor, LocalVariableAsminfo localVariable, AsmLoadable value){
		setter.invoke(localVariable, visitor, value);
	}
	
	public ClassAsminfo getType(){
		return getter.getResultClass();
	}
}
